<?php
class User{
	private $user_id;
	private $firstname;
	private $lastname;
	private $email;
	private $telephone;
	private $fax; 
	
	public function __construct($registry) {
		$this->config = $registry->get('config');
		$this->db = $registry->get('db');
		$this->request = $registry->get('request');
		$this->session = $registry->get('session');
		
		if(isset($this->session->data['user_id'])) {
			$user_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user WHERE user_id='" . (int)$this->session->data['user_id'] . "' AND status = '1'");
			
			if($user_query->num_rows){
				$this->user_id = $user_query->row['user_id'];
				$this->firstname = $user_query->row['firstname'];
				$this->lastname = $user_query->row['lastname'];
				$this->email = $user_query->row['email'];
				$this->telephone = $user_query->row['telephone'];
				$this->fax = $user_query->row['fax'];
				
				$this->db->query("UPDATE " . DB_PREFIX . "user SET ip='" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE user_id='" . (int)$this->session->data['user_id'] . "'");
			}
		} else {
			$this->logout();
		}
	}
	
	
	public function login($email, $password, $override = false) {
		if($override) {
			$user_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user WHERE LOWER(email)='" . $this->db->escape(strtolower($email)) . "' AND status='1'");
		}else{
			$user_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user WHERE LOWER(email)='" . $this->db->escape(strtolower($email)) . "' AND password='" . $this->db->escape(md5($password)) . "' AND status='1'");
		}
		
		if($user_query->num_rows){
			$this->session->data['user_id'] = $user_query->row['user_id'];
			
			$this->user_id = $user_query->row['user_id'];
			$this->firstname = $user_query->row['firstname'];
			$this->lastname = $user_query->row['lastname'];
			$this->email = $user_query->row['email'];
			$this->telephone = $user_query->row['telephone'];
			$this->fax = $user_query->row['fax'];
			
			$this->db->query("UPDATE " . DB_PREFIX . "user SET ip='" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE user_id='" . (int)$this->user_id . "'");
			
			return true;
		} else {
			return false;
		}
	}
	
	public function logout() {
		unset($this->session->data['user_id']);
		
		$this->user_id = '';
		$this->firstname = '';
		$this->lastname = '';
		$this->email = '';
		$this->telephone = '';
		$this->fax = '';
		
	}
	
	public function isLogged() {
		return $this->user_id;
	}
	
	public function getId() {
		return $this->user_id;
	}
	
	public function getFirstName() {
		return $this->firstname;
	}
	
	public function getLastName() {
		return $this->lastname;
	}
	
	public function getEmail() {
		return $this->email;
	}
	
	public function  getTelephone() {
		return $this->telephone;
	}
	
	public function getFax() {
		return $this->fax;
	}
	
	/*
	
	+------------+--------------+------+-----+---------------------+----------------+
	| Field      | Type         | Null | Key | Default             | Extra          |
	+------------+--------------+------+-----+---------------------+----------------+
	| user_id    | int(11)      | NO   | PRI | NULL                | auto_increment |
	| conf_id    | int(11)      | NO   |     | 0                   |                |
	| firstname  | varchar(32)  | NO   |     |                     |                |
	| lastname   | varchar(32)  | NO   |     |                     |                |
	| email      | varchar(96)  | NO   |     |                     |                |
	| telephone  | varchar(32)  | NO   |     |                     |                |
	| fax        | varchar(32)  | NO   |     |                     |                |
	| password   | varchar(40)  | NO   |     |                     |                |
	| ip         | varchar(15)  | NO   |     | 0                   |                |
	| status     | tinyint(1)   | NO   |     | NULL                |                |
	| token      | varchar(255) | NO   |     | NULL                |                |
	| date_added | datetime     | NO   |     | 0000-00-00 00:00:00 |                |
	+------------+--------------+------+-----+---------------------+----------------+
	*/
}